package net.peelmeagrape.hibernate.xmlgen;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import sun.reflect.annotation.AnnotationType;

import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import net.peelmeagrape.hibernate.config.HibernateAnnotationsProcessor;

public class AnnotationUtils
{
    private static Log log = LogFactory.getLog(HibernateAnnotationsProcessor.class);
    static AnnotationType getInternalAnnotationInfo(Annotation annotation)
    {
        Class<? extends Annotation> annotationType = annotation.annotationType();
        return getInternalAnnotationInfo(annotationType);
    }

    static AnnotationType getInternalAnnotationInfo(Class<? extends Annotation> annotationClass)
    {
        try
        {
            Class<? extends Class> aClass = annotationClass.getClass();
            Method[] methods = aClass.getDeclaredMethods();

            Method getAnnotationType=null;
            for (int i = 0; i < methods.length; ++i)
            {
                if (methods[i].getName().equals("getAnnotationType"))
                {
                    getAnnotationType = methods[i];
                }
            }
            if(getAnnotationType!=null)
            {
                getAnnotationType.setAccessible(true);
                return (AnnotationType) getAnnotationType.invoke(annotationClass);
            }
            return null;
        }
        catch (IllegalAccessException e)
        {
            log.debug(e);
            return null;
        }
        catch (InvocationTargetException e)
        {
            log.debug(e);
            return null;
        }
    }
}
